﻿/**************************************************************
 *
 * 唯一标识：237f8131-b7cb-404c-adde-a026a77f10cb
 * 命名空间：Sgr.UPMS.Application.Commands.Organizations
 * 创建时间：2023/8/24 6:30:56
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：创建组织机构命令处理器
 *
 **************************************************************/

using MediatR;
using Sgr.Application.Commands;
using Sgr.Identity.Services;
using Sgr.UPMS.Domain.Organizations;
using Sgr.UPMS.Domain.Users;
using System.Net;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;

namespace Sgr.UPMS.Application.Commands.Organizations
{
    /// <summary>
    /// 创建组织机构命令处理器
    /// </summary>
    public class CreateOrgCommandHandle : IRequestHandler<CreateOrgCommand, CommandResult<bool>>
    {
        private readonly IOrganizationManage _organizationManage;
        private readonly IOrganizationRepository _organizationRepository;
        private readonly IUserManage _userManage;
        private readonly IPasswordHashService _passwordHashService;
        private readonly IUserRepository _userRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="organizationManage">组织机构管理器</param>
        /// <param name="organizationRepository">组织机构仓储</param>
        /// <param name="userManage">用户管理器</param>
        /// <param name="passwordHashService">密码哈希服务</param>
        /// <param name="userRepository">用户仓储</param>
        public CreateOrgCommandHandle(IOrganizationManage organizationManage,
            IOrganizationRepository organizationRepository,
            IUserManage userManage,
            IPasswordHashService passwordHashService,
            IUserRepository userRepository)
        {
            _organizationManage = organizationManage;
            _organizationRepository = organizationRepository;
            _userManage = userManage;
            _passwordHashService = passwordHashService;
            _userRepository = userRepository;
        }

        /// <summary>
        /// 处理创建组织机构命令
        /// </summary>
        /// <param name="request">命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处理结果</returns>
        public async Task<CommandResult<bool>> Handle(CreateOrgCommand request, CancellationToken cancellationToken)
        {
            // 创建组织机构
            Organization organization = await _organizationManage.CreateNewAsync(
                null, // 新建组织机构时ID为null,由系统自动生成
                request.Name,
                request.StaffSizeCode,
                request.Remarks,
                request.ParentId, // 父级ID为null表示顶级组织
                cancellationToken);

            organization.OrgTypeCode = request.OrgTypeCode;
            organization.AreaCode = request.AreaCode;
            organization.Leader = request.Leader;
            organization.Phone = request.Phone;
            organization.Email = request.Email;
            organization.Address = request.Address;
            organization.OrderNumber = request.OrderNumber;

            // 保存组织机构到仓储
            await _organizationRepository.InsertAsync(organization, cancellationToken);

            // 创建管理员用户
            User user = await _userManage.CreateNewAsync(
                request.AdminName,
                _passwordHashService,
                request.AdminPassword,
                true, // 设置为管理员
                organization.Id,
                cancellationToken);

            user.DifferenceUpdateRoles(new long[] { Constant.SYSTEM_ADMIN_ROLE_ID }, true);

            // 保存用户到仓储
            await _userRepository.InsertAsync(user, cancellationToken);

            // 返回成功结果
            return CommandResult.Success(true, "组织机构创建成功");
        }
    }
}